package checkers.client.logic.classes.network;

import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import checkers.common.logic.commands.Command;

public class CommandsSerializer {
	
	private JAXBContext jaxbContext;
	private Marshaller jaxbMarshaller;
	private Unmarshaller jaxbUnmarshaller;
	
	public CommandsSerializer(String contextPath) throws JAXBException {
		jaxbContext = JAXBContext.newInstance(contextPath);
		jaxbMarshaller = jaxbContext.createMarshaller();
		jaxbUnmarshaller = jaxbContext.createUnmarshaller();
	}
	
	public Command getCommandFromStream(InputStream in) throws JAXBException {
		return (Command)jaxbUnmarshaller.unmarshal(in);
	}

	public void writeCommandToStream(OutputStream out, Command command) throws JAXBException {
		jaxbMarshaller.marshal(command, out);
	}
	
}
